package com.yyk.yykywd.mvvm.view_model

import androidx.lifecycle.MutableLiveData
import com.orhanobut.logger.Logger
import com.yyk.yykywd.aa.PieChartBean
import com.yyk.yykywd.mvvm.entity.HomeBean
import com.yyk.yykywd.mvvm.entity.HomeSearchBean
import com.yyk.yykywd.mvvm.model.HomeModel
import java.util.*
import kotlin.collections.ArrayList

/**
 * 文件描述：
 * Created by guo on 2019-11-22.
 */
class BusinessViewModel : BaseViewModel() {
    var id: String = ""
    var reports: MutableList<HomeBean.Data.Report> = ArrayList()
    lateinit var customerStatistics: HomeBean.Data.Count
    var statisticsHospitalPielist: MutableList<PieChartBean> = ArrayList()
    var statisticsHospitalIntegers: MutableList<Int> = ArrayList()
    var statisticsAgentPielist: MutableList<PieChartBean> = ArrayList()
    var statisticsAgentIntegers: MutableList<Int> = ArrayList()
    var hospitalCustomerSum = 0
    var agentCustomerSum = 0
    val startDate: Calendar = Calendar.getInstance()
    val endDate: Calendar = Calendar.getInstance()
    lateinit var hospital: HomeSearchBean.Data.Hos
    lateinit var agent: HomeSearchBean.Data.Agent
    var colors: MutableList<Int> = ArrayList()
    var hospitalPieList: MutableList<PieChartBean> = ArrayList()
    var agentPieList: MutableList<PieChartBean> = ArrayList()
    var hospitalCustomerSum1 = 0
    var agentCustomerSum1 = 0
    var selectedHospitalCustomer = false

    init {
        startDate.set(2019, 10, 1)
    }

    private val homeModel = HomeModel()
    //LiveData,使该数据可以被感知
    var home = MutableLiveData<HomeBean>()
    var search = MutableLiveData<HomeSearchBean>()

    fun getHome() {
        homeModel.getDepartmentList(id, object : HomeModel.GetHome {
            override fun getInfo(home: HomeBean) {
                reports.clear()
                statisticsAgentPielist.clear()
                agentCustomerSum = 0
                statisticsHospitalPielist.clear()
                hospitalCustomerSum = 0
                reports.addAll(home.data.report)
                customerStatistics = home.data.count
                if (id == "0") {
                    hospitalCustomerSum = customerStatistics.hosaddcount +
                            customerStatistics.choscount +
                            customerStatistics.pactcount +
                            customerStatistics.okcount +
                            customerStatistics.ghcount
                    if (hospitalCustomerSum == 0) {
                        statisticsHospitalPielist.add(PieChartBean(1f, "意向客户"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "C类客户"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "签订合同客户"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "安装完成"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "公海客户"))
                    } else {
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.hosaddcount.toFloat(), "意向客户"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.choscount.toFloat(), "C类客户"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.pactcount.toFloat(), "签订合同客户"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.okcount.toFloat(), "安装完成"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.ghcount.toFloat(), "公海客户"))
                    }
                } else {
                    hospitalCustomerSum = customerStatistics.hosaddcount +
                            customerStatistics.choscount +
                            customerStatistics.pactcount +
                            customerStatistics.okcount
                    if (hospitalCustomerSum == 0) {
                        statisticsHospitalPielist.add(PieChartBean(1f, "意向客户"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "C类客户"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "签订合同客户"))
                        statisticsHospitalPielist.add(PieChartBean(1f, "安装完成"))
                    } else {
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.hosaddcount.toFloat(), "意向客户"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.choscount.toFloat(), "C类客户"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.pactcount.toFloat(), "签订合同客户"))
                        statisticsHospitalPielist.add(PieChartBean(customerStatistics.okcount.toFloat(), "安装完成"))
                    }
                }

                if (id == "0") {
                    agentCustomerSum = customerStatistics.dhosaddcount +
                            customerStatistics.dchoscount +
                            customerStatistics.dpactcount +
                            customerStatistics.dmoneyokcount +
                            customerStatistics.dghcount
                    if (agentCustomerSum == 0) {
                        statisticsAgentPielist.add(PieChartBean(1f, "意向客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "C类客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "签订合同客户"))
//                        statisticsAgentPielist.add(PieChartBean(1f, "待回款客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "回款完成客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "公海客户"))
                    } else {
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dhosaddcount.toFloat(), "意向客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dchoscount.toFloat(), "C类客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dpactcount.toFloat(), "签订合同客户"))
//                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dhkcount.toFloat(), "待回款客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dmoneyokcount.toFloat(), "回款完成客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dghcount.toFloat(), "公海客户"))
                    }
                } else {
                    agentCustomerSum = customerStatistics.dhosaddcount +
                            customerStatistics.dchoscount +
                            customerStatistics.dpactcount +
                            customerStatistics.dmoneyokcount
                    if (agentCustomerSum == 0) {
                        statisticsAgentPielist.add(PieChartBean(1f, "意向客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "C类客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "签订合同客户"))
//                        statisticsAgentPielist.add(PieChartBean(1f, "待回款客户"))
                        statisticsAgentPielist.add(PieChartBean(1f, "回款完成客户"))
                    } else {
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dhosaddcount.toFloat(), "意向客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dchoscount.toFloat(), "C类客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dpactcount.toFloat(), "签订合同客户"))
//                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dhkcount.toFloat(), "待回款客户"))
                        statisticsAgentPielist.add(PieChartBean(customerStatistics.dmoneyokcount.toFloat(), "回款完成客户"))
                    }
                }

                this@BusinessViewModel.home.value = home
            }

            override fun onError(e: String) {
                error.value = e
            }

        })
    }

    fun getSearchData(startDate: String, endDate: String) {
        homeModel.getSearchList(id, startDate, endDate, object : HomeModel.GetSearchData {
            override fun getInfo(info: HomeSearchBean) {
                hospital = info.data.hos
                agent = info.data.agent
                Logger.e(agent.toString())
                hospitalPieList.clear()
                hospitalCustomerSum1 = 0
                agentPieList.clear()
                agentCustomerSum1 = 0
                hospitalCustomerSum1 = hospital.hsmall +
                        hospital.hcenter +
                        hospital.hbig
                if(hospitalCustomerSum1 == 0) {
                    hospitalPieList.add(PieChartBean(1f, "小型站点"))
                    hospitalPieList.add(PieChartBean(1f, "中型站点"))
                    hospitalPieList.add(PieChartBean(1f, "大型站点"))
                } else {
                    hospitalPieList.add(PieChartBean(hospital.hsmall.toFloat(), "小型站点"))
                    hospitalPieList.add(PieChartBean(hospital.hcenter.toFloat(), "中型站点"))
                    hospitalPieList.add(PieChartBean(hospital.hbig.toFloat(), "大型站点"))
                }
                agentCustomerSum1 = agent.asmall +
                        agent.acenter +
                        agent.abig
                if (agentCustomerSum1 == 0) {
                    agentPieList.add(PieChartBean(1f, "小型站点"))
                    agentPieList.add(PieChartBean(1f, "中型站点"))
                    agentPieList.add(PieChartBean(1f, "大型站点"))
                } else {
                    agentPieList.add(PieChartBean(agent.asmall.toFloat(), "小型站点"))
                    agentPieList.add(PieChartBean(agent.acenter.toFloat(), "中型站点"))
                    agentPieList.add(PieChartBean(agent.abig.toFloat(), "大型站点"))
                }
                search.value = info
            }

            override fun onError(e: String) {
            }

        })
    }
}